import { Component, OnInit } from '@angular/core';
import { WarnService } from 'src/app/service/warn.service';
import { WarningService } from 'src/app/service/warning.service';

@Component({
    selector: 'app-alarm-mgmt',
    templateUrl: './alarm-mgmt.component.html',
    styleUrls: ['./alarm-mgmt.component.scss'],
})
export class AlarmMgmtComponent implements OnInit {
    data: any[];
    searchName: any;
    searchState: any;
    selectTime: any[] = [new Date(Date.now() - 86400000 * 720), new Date()];
    state: any = [
        { label: '已处理', value: 1 },
        { label: '未处理', value: 0 },
    ];
    constructor(
        private warnService: WarnService,
        private warningService: WarningService
    ) {}
    genTwoLengthNumberString = (n) => (n >= 10 ? n : '0' + n);
    transStatus = (s) => {
        if (s === '未处理') {
            return 0;
        } else if (s === '已处理') {
            return 1;
        }
        return null;
    };
    type = ['水量超额报警', '其他报警'];
    transForm = (s) => {
        if (s === 0) {
            return '未处理';
        } else if (s === 1) {
            return '已处理';
        } else {
            return '未知状态';
        }
    };
    async search() {
        console.log(this.searchName)
        console.log(this.searchState)
        console.log(this.selectTime)
        const warningList = await this.warningService.fetchWarningsList({
            applicantName: this.searchName || undefined,
            status: this.searchState,
            startTime: this.dateTimeTrans(this.selectTime[0]),
            endTime: this.dateTimeTrans(this.selectTime[1]),
            wellName: null,
        });
        console.log('warningList.data', warningList.data);
        this.data = warningList.data;
    }
    reset(){
        this.searchName = ''
        this.searchState = ''
        this.selectTime = [new Date(Date.now() - 86400000 * 720), new Date()]
        this.search()
    }
    dateTimeTrans(d) {
        if (!d) {
            return '';
        }
        let yy = d.getFullYear();
        let MM = this.genTwoLengthNumberString(d.getMonth() + 1);
        let dd = this.genTwoLengthNumberString(d.getDate());
        let hh = this.genTwoLengthNumberString(d.getHours());
        let mm = this.genTwoLengthNumberString(d.getMinutes());
        let ss = this.genTwoLengthNumberString(d.getSeconds());
        return yy + '-' + MM + '-' + dd + ' ' + hh + ':' + mm + ':' + ss;
    }
    handleStatus = async (item) => {
        console.log(item);
        let res;
        if (item.status === 1) {
            res = await this.warnService.updateWarnIdById({
                id: item.warnId,
                well_id: item.wellId,
                status: 0,
            });
        } else if (item.status === 0) {
            res = await this.warnService.updateWarnIdById({
                id: item.warnId,
                well_id: item.wellId,
                status: 1,
            });
        }
        if (res.code == 200) {
            this.search()
        }
    };

    ngOnInit(): void {
        this.search();
        /*this.data = [
            {
                warnId: '1121',
                wellName: '1111',
                applicantName: '2662',
                warnType: '0',
                address: 'ffewf',
                status: 0,
                warnTime: '424424',
            },
            {
                warnId: '1121',
                wellName: '1111',
                applicantName: '2662',
                warnType: '0',
                address: 'ffewf',
                status: 0,
                warnTime: '424424',
            },
            {
                warnId: '1121',
                wellName: '1111',
                applicantName: '2662',
                warnType: '0',
                address: 'ffewf',
                status: 1,
                warnTime: '424424',
            },
        ];*/
    }
}
